package com.vert.utils;

import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.commons.io.monitor.FileAlterationObserver;

import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.util.concurrent.TimeUnit;

/**
 * @Classname FileWatcher
 * @Description TODO
 * @Date 2022/6/5 19:58
 * @Created by XiongXiong
 * @Author: X.I.O
 */
public class FileWatcher {

  //目录监听
  public static void init(){
     listener();
  }

  public static void listener(){
    // 轮询间隔 5 秒
    long interval = TimeUnit.SECONDS.toMillis(1);
    //不使用过滤器,使用过滤器时创建FileAlterationObserver对象时传入第二个参数FileFilter
    FileAlterationObserver observer = new FileAlterationObserver(new File("D:\\code\\start-base\\target"));
    FileListener fileListener = new FileListener();
    observer.addListener(fileListener);
    //创建文件变化监听器
    FileAlterationMonitor monitor = new FileAlterationMonitor(interval, observer);
    // 开始监控
    try {
      monitor.start();
    }catch (Exception e){}

  }



}
